package com.yanqu.road.dao.impl.channelcenter.luckydraw;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.luckydraw.LuckyDrawRecord;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LuckyDrawRecordDaoImpl extends TempDataObjectDao<LuckyDrawRecord> {

    public static String getLuckyDrawRecordSql = "select * from `t_n_lucky_draw_record` where `activityId`=? order by `time` asc";

    public List<LuckyDrawRecord> getLuckyDrawRecord(int activityId) {
        List<LuckyDrawRecord> list = new ArrayList<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getLuckyDrawRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    @Override
    protected boolean updateData(LuckyDrawRecord luckyDrawRecord) {
        return false;
    }

    public static String insertLuckyDrawRecordSql = "insert into `t_n_lucky_draw_record`(activityId,channelId,serverId,userId,nickname,reward,goodsId,time) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(LuckyDrawRecord luckyDrawRecord) {
        luckyDrawRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawRecord.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawRecord.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawRecord.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, luckyDrawRecord.getNickname()));
        para.put(i++, new DbParameter(Types.VARCHAR, luckyDrawRecord.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawRecord.getGoodsId()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawRecord.getTime()));
        if (getDb().executeNoneQuery(insertLuckyDrawRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected LuckyDrawRecord parseData(ResultSet rs) throws SQLException {
        LuckyDrawRecord luckyDrawRecord = new LuckyDrawRecord();
        luckyDrawRecord.setActivityId(rs.getInt("activityId"));
        luckyDrawRecord.setChannelId(rs.getInt("channelId"));
        luckyDrawRecord.setServerId(rs.getLong("serverId"));
        luckyDrawRecord.setUserId(rs.getLong("userId"));
        luckyDrawRecord.setNickname(rs.getString("nickname"));
        luckyDrawRecord.setReward(rs.getString("reward"));
        luckyDrawRecord.setGoodsId(rs.getInt("goodsId"));
        luckyDrawRecord.setTime(rs.getLong("time"));
        return luckyDrawRecord;
    }

}
